native = function() {}



// 小明 appId a4392f70-923f-44c4-9f5b-abb25ccb181a
// 小玲 appId 46c2c229-dc21-433d-93cb-58762475705a

//服务器地址
var burl=window.location.href;
console.log(burl);

if (burl.indexOf("test") > 0) {
	// 测试环境
	URLS.SERVER_URL = "https://apptest.www.com:7018/";
	URLS.SERVER_XM_URL = "https://apptest.www.com:7018/tp/";
	URLS.key="";
	URLS.secret="";
}else if (burl.indexOf("dev.www.com:7788") > 0 || burl.indexOf("58.220.195.111:7788") > 0) {
	// 开发环境
	URLS.SERVER_URL = "https://dev.www.com:8888/";
	URLS.SERVER_XM_URL = "https://apptest.www.com:7018/tp/";
	URLS.key="";
	URLS.secret="";
}else{
	// 正式环境
	URLS.SERVER_URL = "https://app.www.com:7018/";
	URLS.SERVER_XM_URL = "https://xm.www.com:7023/";
	URLS.key="";
	URLS.secret="";
}



//判断设备
var ua = navigator.userAgent.toLowerCase();
if (/iphone|ipad|ipod/.test(ua)) {
	native.platfrom = "ios";
	setTimeout(function(){ioss();},20)
}else if (/harmonyos/.test(ua)) {
	native.platfrom = "harmonyos";
}else if (/android/.test(ua)) {
	native.platfrom = "android";
}





function ioss(){
	// console.log('burl.indexOf("yes=")',burl.indexOf('yes='))
	// if (burl.indexOf("yes=") == -1) {
	// 	let t = new Date();
	// 	if(burl.indexOf("?") == -1){
	// 		window.location.href = burl+"?yes="+t.getTime();
	// 	}else{
	// 		window.location.href = burl+"&yes="+t.getTime();
	// 	}
	// }
}


/*这段代码是IOS固定的，必须要放到js中*/
var setupWebViewJavascriptBridge = function(callback) {
	if (window.WebViewJavascriptBridge) {
		return callback(WebViewJavascriptBridge);
	}
	if (window.WVJBCallbacks) {
		return window.WVJBCallbacks.push(callback);
	}
	window.WVJBCallbacks = [callback];
	var WVJBIframe = document.createElement('iframe');
	WVJBIframe.style.display = 'none';
	WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
	document.documentElement.appendChild(WVJBIframe);
	setTimeout(function() {
		document.documentElement.removeChild(WVJBIframe)
	}, 0)
};

//与原生交互
native.nativeFun = function(params) {
	console.log(params);
	if (native.platfrom === "android") {
		// params = JSON.stringify(params);	
		console.log("android:",params);
		console.log("params.funName:",params.funName);
		console.log("params.params:",params.params);
		let funValue = params.params;
		// console.log(typeof(funValue));

		funValue =  JSON.stringify(funValue);
		// console.log(funValue);
		// console.log(typeof(funValue));

		var funName = params.funName;
		//android
		// window.android[params.funName]();

		if(params.funName == "h5_goback"){
			window.android[params.funName]();
		}else{
			window.android[params.funName](funValue);
		}
		
		//JSInterface.nativeFunction(params);
	} else if (native.platfrom === "ios") {
		//ios
		console.log("ios:",params);
		setupWebViewJavascriptBridge(function(bridge) {
			bridge.callHandler(params.funName, params.params, function responseCallback(responseData) {
				console.log("JS received response:", responseData)
			})
		})
	} else if (native.platfrom === "harmonyos") {
		let funValue = params.params;
		// console.log(typeof(funValue));

		funValue =  JSON.stringify(funValue);

		window.carlift.call(funValue)

	}

}


  


//ajax请求公用函数
function ajaxPost(url,data,success,type,error,timeout){
	//设置请求超时时间
	if(!timeout){
		timeout = 10000;
	}
	
	$.ajax({
		headers: {
			// "token":token,
			"serviceToken":localStorage.serviceToken
		},
		type: type?type:'POST',
		timeout : timeout,
		url: url ,
		data: data ,
		// xhrFields: {
        //     withCredentials: true
        // },
        crossDomain: true,

		// async:false,
		// cache: true,
		// processData: false,	

		success: function(data){
			// console.log("dddd:",data);  
			success(data);
		} ,
		error : function(data){
			console.log(data); 
			if ((data.status && data.status == '401') || (data.statusText && data.statusText == 'No Transport')) {
				var param = {};
				param["refreshServiceToken"] = localStorage.serviceRefreshToken || GetQueryString("serviceRefreshToken");
				
				var paramsStr = '';
				for (v in param) {
					paramsStr = paramsStr + v + '=' + param[v] + '&';
				}
				console.log(URLS.refreshServiceToken + '?' + paramsStr.substr(0, paramsStr.length - 1));

				$.ajax({
					type: 'POST',
					timeout : 1000,
					url: URLS.refreshServiceToken,
					data: param,
					xhrFields: {
						withCredentials: true
					},
					crossDomain: true,
					success: function(data){
						console.log(data);
						//console.log(data.datas.totalScore);
						if(data.code == 1){
							localStorage.setItem('serviceToken', data.datas.serviceToken);
							localStorage.setItem('serviceRefreshToken', data.datas.refreshServiceToken);
							var currentUrl = window.location.href;
            				window.location.href = currentUrl;
						}else if(data.code == 0){
							// window.location.href="login.html";
						} if(data.code == "-1"){
							var jsonParams = {
								'funName': 'newToken',
								'params': {
									'nav': "",
								}
							};
							native.nativeFun(jsonParams);
							// window.location.href="login.html";
						}else{
							// window.location.reload();
							$(".ajax_noload").remove();
							$("<div data-sensors-click class='ajax_noload'><i class='iconfont icon-wifi g0'></i>网络请求失败，请稍候再试<i class='iconfont icon-shuaxin2 r'></i></div>").prependTo("body");
							$(".ajax_noload").show();
							console.log(data.msg);
						}	
					} ,
					error : function(){
						// window.location.reload();
						$(".ajax_noload").remove();
						$("<div data-sensors-click class='ajax_noload'><i class='iconfont icon-wifi g0'></i>网络请求失败，请稍候再试<i class='iconfont icon-shuaxin2 r'></i></div>").prependTo("body");
						$(".ajax_noload").show();
					},
					dataType: 'json'
				});
			}else{
				// window.location.reload();
				$(".ajax_noload").remove();
				$("<div data-sensors-click class='ajax_noload'><i class='iconfont icon-wifi g0'></i>网络请求失败，请稍候再试<i class='iconfont icon-shuaxin2 r'></i></div>").prependTo("body");
				$(".ajax_noload").show();
				$(".ajax_noload").unbind('click').click(function () {
					localStorage.setItem('refreshed', 'false');
					window.location.reload();
				})
			}
			//error();
		},
		dataType: 'json'
	});
}


////刷新页面
$(".ajax_noload").unbind('click').click(function () {
	localStorage.setItem('refreshed', 'false');
	window.location.reload();
//	var jsonParams = {
//		'funName': 'reload_web_fun',
//		'params': {}
//	};
//	native.nativeFun(jsonParams);
})



